﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SolutionBLL;
using SolutionModel;
using SFast;
using SFast.Web;

public partial class admin_system_RolesDetail : System.Web.UI.Page
{

    T_AdminRolesBLL bll = new T_AdminRolesBLL();
    protected String btn_text = "提交";


    protected void Page_Load(object sender, EventArgs e)
    {
        String act = Request.QueryString["act"];  //url中的act
        String Action = Request.Form["action"];   //表单中的action

        #region 页面初始化
        //页面添加时初始化加载
        if (act == "add" && Action == null)
        {
            this.action.Value = "addSave";
            btn_text = "提  交";
        }

        //页面编辑时初始化加载
        if (act == "edit" && Action == null)
        {
            String id = String.Empty;
            try
            {
                id = Request.QueryString["id"];
            }
            catch (Exception emsg)
            {
                Response.Write("参数有误，如无法解决请联系技术人员！信息：" + emsg.Message);
                Response.End();
            }
            btn_text = "保  存";
            this.action.Value = "editSave";
            bll.GetAdminRolesByIDToJs(id);
        }

        //检查角色名称是否已存在
        if (act == "checkingName" && Action == null)
        {
            String Account = Request["param"];
            String id = Request.QueryString["id"];
            int rs = bll.HasRolesName(Account , id);
            if (rs > 0)
            {
                Response.Write("对不起，此角色不可用或已存在！");
            }
            else
            {
                Response.Write("y");
            }
            Response.End();
        }
        #endregion


        #region 按钮事件

        #region 添加页面“提交”按钮
        if (Action == "addSave")
        {
            try
            {
                T_AdminRole obj = Request._GetForm<T_AdminRole>();
                obj.RoleID = Guid.NewGuid().ToString();
                obj.CreateTime = DateTime.Now;
                //添加前检查当前在添加的角色名称是否已经存在
                int re = bll.HasRolesName(obj.RoleName , "");
                if (re > 0)
                {
                    Response.Write("{\"info\":\"对不起，角色名称不可用，可能在数据中已经存在！\",\"status\":\"n\"}");
                    Response.End();
                }

                //添加用户
                bll.InsertAdminRoles(obj);
                Response.Write("{\"info\":\"角色添加成功！\",\"status\":\"y\"}");
            }
            catch (Exception emsg)
            {
                Response.Write("{\"info\":\"" + emsg.Message + "\",\"status\":\"n\"}");
            }
            finally
            {
                Response.End();
            }

        }
        #endregion

        #region 编辑页面 “ 保存” 按钮事件
        if (Action == "editSave")
        {
            try
            {
                var obj=Request._GetForm<T_AdminRole>();
                var rs= bll.HasRolesName(obj.RoleName, obj.RoleID);
                if (rs>0)
                {
                    throw new Exception("角色名称不可用或数据库中已存在。");
                }
                bll.UpdateAdminRoles(obj);
                Response.Write("{\"info\":\"角色保存成功！\",\"status\":\"y\"}");
            }
            catch (Exception emsg)
            {
                Response.Write("{\"info\":\"" + emsg.Message + "\",\"status\":\"n\"}");
            }
            finally
            {
                Response.End();
            }
        }
        #endregion

        #endregion
    }
}